# Loading Packages and Data
library(tidyverse)
library(maps)
library(leaflet)
library(sf)
library(gridExtra)
library(grid)
# Loads ftprint data
ftprint <- read_csv("NFA 2018.csv")
# Creating world that has geometry for all countries
world <- maps::map("world", plot = FALSE, fill = TRUE, labels = TRUE) %>%
sf::st_as_sf() %>%
sf::st_transform(4326)
# Chart Theme
chart_theme <- theme(
plot.title = element_text(family = "Arial", face = "bold", size = (12.5)),
legend.title = element_text(face = "italic", family = "Verdana"),
legend.text = element_text(face = "italic", family = "Verdana", size = (8)),
axis.title = element_text(family = "Arial", size = (11)),
axis.text = element_text())
# Data Wrangling
# Renaming ftprint countries to standardize naming scheme
ftprint$`country`[ftprint$`country` == "United States of America"] <- "USA"
ftprint$`country`[ftprint$`country` == "Venezuela, Bolivarian Republic of"] <- "Venezuela"
ftprint$`country`[ftprint$`country` == "Viet Nam"] <- "Vietnam"
ftprint$`country`[ftprint$`country` == "Antigua and Barbuda"] <- "Antigua"
ftprint$`country`[ftprint$`country` == "Brunei Darussalam"] <- "Brunei"
ftprint$`country`[ftprint$`country` == "Cabo Verde"] <- "Cape Verde"
ftprint$`country`[ftprint$`country` == "Congo"] <- "Republic of Congo"
ftprint$`country`[ftprint$`country` == "Iran, Islamic Republic of"] <- "Iran"
ftprint$`country`[ftprint$`country` == "Côte d'Ivoire"] <- "Ivory Coast"
ftprint$`country`[ftprint$`country` == "Korea, Democratic People's Republic of"] <- "North Korea"
ftprint$`country`[ftprint$`country` == "Korea, Republic of"] <- "South Korea"
ftprint$`country`[ftprint$`country` == "Lao People's Democratic Republic"] <- "Laos"
ftprint$`country`[ftprint$`country` == "Libyan Arab Jamahiriya"] <- "Libya"
ftprint$`country`[ftprint$`country` == "Macedonia TFYR"] <- "Macedonia"
ftprint$`country`[ftprint$`country` == "Micronesia, Federated States of"] <- "Micronesia"
ftprint$`country`[ftprint$`country` == "USSR"] <- "Russian Federation"
ftprint$`country`[ftprint$`country` == "Russian Federation"] <- "Russia"
ftprint$`country`[ftprint$`country` == "Syrian Arab Republic"] <- "Syria"
ftprint$`country`[ftprint$`country` == "Réunion"] <- "Reunion"
ftprint$`country`[ftprint$`country` == "Tanzania, United Republic of"] <- "Tanzania"
ftprint$`country`[ftprint$`country` == "Trinidad and Tobago"] <- "Trinidad"
ftprint$`country`[ftprint$`country` == "United Kingdom"] <- "UK"
ftprint$`country`[ftprint$`country` == "Congo, Democratic Republic of"] <- "Democratic Republic of the Congo"
# Creating Population for each UN Region as new df
ftprint_UN_carbon <- ftprint %>%
filter(year == 2014 & record == "EFConsPerCap") %>%
filter(UN_region %in% c("Africa",
"Asia",
"Europe",
"Latin America and the Caribbean",
"North America",
"Oceania")) %>%
group_by(UN_region) %>%
mutate(UN_region_avg = mean(carbon, na.rm = TRUE)) %>%
ungroup() %>%
mutate(UN_region = recode(UN_region,
`Latin America and the Caribbean` = "Lat. America &\nCaribbean")) %>%
select(UN_region, UN_region_avg) %>%
unique() %>%
mutate(UN_region = reorder(UN_region, UN_region_avg))
# DF's for Carbon Emissions for the years:
# 2014
# Select a subsection of variables from ftprint
ftprint_map_2014 <- ftprint %>%
select(ID = starts_with("country"),
year,
record,
carbon,
population,
crop_land,
built_up_land,
fishing_ground,
forest_land,
grazing_land,
UN_region,
`Percapita GDP (2010 USD)`) %>%
filter(record == "EFConsPerCap") %>%
filter(year == 2014)
# Joining world with ftprint_map
ftprint_world_sf_2014 <- world %>%
left_join(ftprint_map_2014, by = "ID")
# 2010
# Select a subsection of variables from ftprint
ftprint_map_2010 <- ftprint %>%
select(ID = starts_with("country"),
year,
record,
carbon,
population,
crop_land,
built_up_land,
UN_region,
`Percapita GDP (2010 USD)`) %>%
filter(record == "EFConsPerCap") %>%
filter(year == 2010)
# Joining world with ftprint_map
ftprint_world_sf_2010 <- world %>%
left_join(ftprint_map_2010, by = "ID")
# 2000
# Select a subsection of variables from ftprint
ftprint_map_2000 <- ftprint %>%
select(ID = starts_with("country"),
year,
record,
carbon,
population,
crop_land,
built_up_land,
UN_region,
`Percapita GDP (2010 USD)`) %>%
filter(record == "EFConsPerCap") %>%
filter(year == 2000)
# Joining world with ftprint_map
ftprint_world_sf_2000 <- world %>%
left_join(ftprint_map_2000, by = "ID")
# 1990
# Select a subsection of variables from ftprint
ftprint_map_1990 <- ftprint %>%
select(ID = starts_with("country"),
year,
record,
carbon,
population,
crop_land,
built_up_land,
UN_region,
`Percapita GDP (2010 USD)`) %>%
filter(record == "EFConsPerCap") %>%
filter(year == 1990)
# Joining world with ftprint_map
ftprint_world_sf_1990 <- world %>%
left_join(ftprint_map_1990, by = "ID")
# 1980
# Select a subsection of variables from ftprint
ftprint_map_1980 <- ftprint %>%
select(ID = starts_with("country"),
year,
record,
carbon,
population,
crop_land,
built_up_land,
UN_region,
`Percapita GDP (2010 USD)`) %>%
filter(record == "EFConsPerCap") %>%
filter(year == 1980)
# Joining world with ftprint_map
ftprint_world_sf_1980 <- world %>%
left_join(ftprint_map_1980, by = "ID")
# 1970
# Select a subsection of variables from ftprint
ftprint_map_1970 <- ftprint %>%
select(ID = starts_with("country"),
year,
record,
carbon,
population,
crop_land,
built_up_land,
UN_region,
`Percapita GDP (2010 USD)`) %>%
filter(record == "EFConsPerCap") %>%
filter(year == 1970)
# Joining world with ftprint_map
ftprint_world_sf_1970 <- world %>%
left_join(ftprint_map_1970, by = "ID")
# 1961
# Select a subsection of variables from ftprint
ftprint_map_1961 <- ftprint %>%
select(ID = starts_with("country"),
year,
record,
carbon,
population,
crop_land,
built_up_land,
UN_region,
`Percapita GDP (2010 USD)`) %>%
filter(record == "EFConsPerCap") %>%
filter(year == 1961)
# Joining world with ftprint_map
ftprint_world_sf_1961 <- world %>%
left_join(ftprint_map_1961, by = "ID")
# Making a df for each Top 5 Emitter
USA_graph_EF <- ftprint %>%
filter(country == "USA" & record == "EFConsPerCap") %>%
gather(key = "Type of Land", value = "gha", crop_land:built_up_land)
China_graph_EF <- ftprint %>%
filter(country == "China" & record == "EFConsPerCap") %>%
gather(key = "Type of Land", value = "gha", crop_land:built_up_land)
Japan_graph_EF <- ftprint %>%
filter(country == "Japan" & record == "EFConsPerCap") %>%
gather(key = "Type of Land", value = "gha", crop_land:built_up_land)
Russia_graph_EF <- ftprint %>%
filter(country == "Russia" & record == "EFConsPerCap") %>%
gather(key = "Type of Land", value = "gha", crop_land:built_up_land)
India_graph_EF <- ftprint %>%
filter(country == "India" & record == "EFConsPerCap") %>%
gather(key = "Type of Land", value = "gha", crop_land:built_up_land)
# Making a df from the Top 5 emitters
graph_EF <- ftprint %>%
filter(country %in% c("China", "USA", "India", "Russia", "Japan") & record == "EFConsPerCap")
# Create interactive map of Carbon over Years
# Creating Palette
pal <- colorQuantile("YlOrRd", NULL, n = 9)
# Creating Popups
carbon_em_2014_pop <- paste0(ftprint_world_sf_2014$ID,
"<br><strong>Carbon Emissions (gha): </strong>",
ftprint_world_sf_2014$carbon)
carbon_em_2010_pop <- paste0(ftprint_world_sf_2010$ID,
"<br><strong>Carbon Emissions (gha): </strong>",
ftprint_world_sf_2010$carbon)
carbon_em_2000_pop <- paste0(ftprint_world_sf_2000$ID,
"<br><strong>Carbon Emissions (gha): </strong>",
ftprint_world_sf_2000$carbon)
carbon_em_1990_pop <- paste0(ftprint_world_sf_1990$ID,
"<br><strong>Carbon Emissions (gha): </strong>",
ftprint_world_sf_1990$carbon)
carbon_em_1980_pop <- paste0(ftprint_world_sf_1980$ID,
"<br><strong>Carbon Emissions (gha): </strong>",
ftprint_world_sf_1980$carbon)
carbon_em_1970_pop <- paste0(ftprint_world_sf_1970$ID,
"<br><strong>Carbon Emissions (gha): </strong>",
ftprint_world_sf_1970$carbon)
carbon_em_1961_pop <- paste0(ftprint_world_sf_1961$ID,
"<br><strong>Carbon Emissions (gha): </strong>",
ftprint_world_sf_1961$carbon)
# The Map
leaflet() %>%
setView(lng = 0, lat = 10, zoom = 1.49) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = ftprint_world_sf_2014,
fillColor = ~pal(carbon), # Adds Polygons with carbon data (2014)
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1,
smoothFactor = 0.5,
popup = carbon_em_2014_pop,
group = "Carbon Emissions 2014") %>%
addPolygons(data = ftprint_world_sf_2010,
fillColor = ~pal(carbon), # Adds Polygons with carbon data (2010)
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1,
smoothFactor = 0.5,
popup = carbon_em_2010_pop,
group = "Carbon Emissions 2010") %>%
addPolygons(data = ftprint_world_sf_2000,
fillColor = ~pal(carbon), # Adds Polygons with carbon data (2000)
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1,
smoothFactor = 0.5,
popup = carbon_em_2000_pop,
group = "Carbon Emissions 2000") %>%
addPolygons(data = ftprint_world_sf_1990,
fillColor = ~pal(carbon), # Adds Polygons with carbon data (1990)
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1,
smoothFactor = 0.5,
popup = carbon_em_1990_pop,
group = "Carbon Emissions 1990") %>%
addPolygons(data = ftprint_world_sf_1980,
fillColor = ~pal(carbon), # Adds Polygons with carbon data (1980)
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1,
smoothFactor = 0.5,
popup = carbon_em_1980_pop,
group = "Carbon Emissions 1980") %>%
addPolygons(data = ftprint_world_sf_1970,
fillColor = ~pal(carbon), # Adds Polygons with carbon data (1970)
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1,
smoothFactor = 0.5,
popup = carbon_em_1970_pop,
group = "Carbon Emissions 1970") %>%
addPolygons(data = ftprint_world_sf_1961,
fillColor = ~pal(carbon), # Adds Polygons with carbon data (1961)
fillOpacity = 0.9,
color = "#BDBDC3",
weight = 1,
smoothFactor = 0.5,
popup = carbon_em_1961_pop,
group = "Carbon Emissions 1961") %>%
addLayersControl(
baseGroups = c("Carbon Emissions 2014","Carbon Emissions 2010","Carbon Emissions 2000", "Carbon Emissions 1990", "Carbon Emissions 1980", "Carbon Emissions 1970", "Carbon Emissions 1961"),
options = layersControlOptions(collapsed = FALSE)
)